package com.bsth.archive;

import java.sql.SQLException;
import java.util.HashMap;

import com.bsth.archive.util.DBUtil;

public class AddUser {
	/**
	 * 有些单位两个名字不一样，
	 * 如 花木街道
	 * 在部门里面，叫花木街道办事处
	 * 在基本信息里面叫 花木街道
	 * 查第一个名字：select * from bsth_sys_department t WHERE dept_name	LIKE '%花木%'
	 * 查第二个名字：select zj from pdda_catalog_t_jddwjbxx t WHERE zj	LIKE '%花木%'
	 */
	final String fatherDpetName = "惠南镇";//用来查部门的
	final String townName = "惠南镇";//用来加入基本信息的
	final String sonName = "海燕居委";
	final String sonQzh = "289_1";
	private DBUtil db;
	public AddUser() {
		db = new DBUtil();
	}

	public static void main(String[] args) {
		AddUser oUser = new AddUser();
		String deptId = oUser.addDept();//添加部门
		String userId = oUser.addUser(deptId);//添加用户
		oUser.addRole(userId, "12");//添加角色
		oUser.addBaseInfo();//添加基本信息
		oUser.addCjwUser();//添加村居委系统相应用户
	}

	public DBUtil getDb() {
		return db;
	}

	public void setDb(DBUtil db) {
		this.db = db;
	}

	/**
	 * 添加部门
	 *
	 * @return 部门ID
	 */
	private String addDept() {
		System.out.println("目前添加用户为：" + sonName + "，全宗号为：" + sonQzh);
		String sonDeptId = "0";
		try {
			/**
			 * 取得上级部门的ID
			 */
			String sql = "select ID from bsth_sys_department t WHERE dept_name = '" + fatherDpetName + "'";
			HashMap map = db.queryMap(db.getConnection(), sql);
			String fatherDeptId = map.get("ID") + "";

			/**
			 * 查询部门是否存在
			 */
			sql = "select ID from bsth_sys_department t WHERE dept_name = '" + sonName + "'";
			map = db.queryMap(db.getConnection(), sql);
			if (map == null || map.size() <= 0) {//不存在则添加部门
				//取得序列号
				String seq = getSeq("SELECT BSTH_SYS_DEPT_SEQ.NEXTVAL AS ID FROM DUAL");
				sql = "INSERT INTO bsth_sys_department(id, parent_id, depth, dept_name,dept_desc) " +
						"VALUES(" + seq + ", " + fatherDeptId + ", 3, '" + sonName + "','" + sonQzh + "')";
				db.execute(db.getConnection(), sql);
				System.out.println("添加部门成功！");
			} else {
				System.out.println("部门已存在~~~");
			}
			//取到部门ID
			sql = "select ID from bsth_sys_department t WHERE dept_name = '" + sonName + "'";
			map = db.queryMap(db.getConnection(), sql);
			if (map != null) {
				sonDeptId = map.get("ID") + "";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sonDeptId;
	}

	/**
	 * 添加用户
	 *
	 * @return 用户ID
	 */
	private String addUser(String sonDeptId) {
		String userId = "0";
		try {
			String sql;
			if (!"0".equals(sonDeptId) && sonDeptId != null) {
				/**
				 * 查询用户是否存在
				 */
				sql = "SELECT * FROM bsth_sys_user  WHERE qzh = '" + sonQzh + "'";
				HashMap map = db.queryMap(db.getConnection(), sql);
				if (map == null || map.size() <= 0) {
					String userSeqId = getSeq("SELECT BSTH_SYS_USER_SEQ.NEXTVAL AS id FROM DUAL");
					String seqId = getSeq("SELECT BSTH_SYS_USEROFSEQ_SEQ.NEXTVAL AS ID FROM DUAL");
					String newPassword = encodePassword(sonQzh);
					sql = "INSERT INTO bsth_sys_user(id, dept_id, realname, loginname, password, sequence, state,qzh,type,bz) " +
							"VALUES(" + userSeqId + ", " + sonDeptId + "" +
							", '" + sonName + "', '" + sonQzh + "', '" + newPassword + "', " + seqId + ", 1,'" + sonQzh + "',1,'" + sonQzh + "') ";
					db.execute(db.getConnection(), sql);
					System.out.println("添加用户成功！");
				} else {
					sql = "UPDATE bsth_sys_user SET dept_id = " + sonDeptId + " WHERE qzh = '" + sonQzh + "'";
					db.execute(db.getConnection(), sql);
					System.out.println("用户已存在~~~");
				}

			}
			//取得用户ID
			sql = "	SELECT ID FROM bsth_sys_user WHERE qzh = '" + sonQzh + "'";
			HashMap map = db.queryMap(db.getConnection(), sql);
			if (map != null) {
				userId = map.get("ID") + "";
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return userId;
	}

	/**
	 * 添加角色
	 * roleId = 12 村居委
	 *
	 * @return
	 */
	private void addRole(String userId, String roleId) {
		try {
			/**
			 * 查询角色是否存在
			 */
			String sql = "SELECT * FROM BSTH_SYS_ROLE_USER  WHERE user_id = '" + userId + "'";
			HashMap map = db.queryMap(db.getConnection(), sql);
			if (map == null || map.size() <= 0) {
				sql = "INSERT INTO BSTH_SYS_ROLE_USER (Role_Id,User_Id)" +
						"VALUES(" + roleId + "," + userId + ")";
				db.execute(db.getConnection(), sql);
				System.out.println("角色添加成功！");
			} else {
				System.out.println("角色已存在~~~");
			}


		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 添加基本信息
	 */
	private void addBaseInfo() {
		try {
			String sql;
			/**
			 * 查询角色是否存在
			 */
			sql = "SELECT * FROM pdda_catalog_t_jddwjbxx  WHERE qzh = '" + sonQzh + "'";
			HashMap map = db.queryMap(db.getConnection(), sql);
			if (map == null || map.size() <= 0) {
				sql = "insert into pdda_catalog_t_jddwjbxx (BM,NAME,SJDW,SORT,ZJ,QZH,KIND) values " +
						"((select max(to_number(bm))+1 from pdda_catalog_t_jddwjbxx),'" + sonName + "','" + townName + "人民政府'," +
						"2,'" + townName + "','" + sonQzh + "','村委会')";
				db.execute(db.getConnection(), sql);
				System.out.println("基本信息添加成功！");
			} else {
				System.out.println("基本信息已存在~~~");
			}


		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 添加村居委系统中的用户
	 */
	private void addCjwUser() {
		try {
			String sql;
			/**
			 * 查询角色是否存在
			 */
			sql = "SELECT * FROM cjw.cjwuser WHERE username = '" + sonQzh + "'";
			HashMap map = db.queryMap(db.getConnection(), sql);
			if (map == null || map.size() <= 0) {
				sql = "INSERT INTO 	cjw.cjwuser(ID,username,realname,PASSWORD,qzh,TYPE)" +
						"VALUES((SELECT max(to_number(ID))+1 FROM cjw.cjwuser),'" + sonQzh + "','"
						+ sonName + "','" + sonQzh + "','" + sonQzh + "',2)";
				db.execute(db.getConnection(), sql);
				System.out.println(sonName + "-" + sonQzh + "-CJW用户添加成功！");
			} else {
				System.out.println(sonName + "-" + sonQzh + "-CJW用户已存在~~~");
			}


		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 取得序列
	 *
	 * @param sql
	 * @return
	 */
	private String getSeq(String sql) {
		String seq = "0";
		try {
			HashMap map = db.queryMap(db.getConnection(), sql);
			if (map != null) {
				//取得序列号
				seq = map.get("ID") + "";
			}
			if (seq.equals("0")) {
				throw new SQLException();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return seq;
	}

	/**
	 * 给密码加密码
	 *
	 * @param
	 */
	private String encodePassword(String password) {
		String code = "";
		PasswdUtil pass = new PasswdUtil();
		code = pass.StrToChar(password);
		return code;
	}

	public String getFatherDpetName() {
		return fatherDpetName;
	}
}

